package stations.QuakeInfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.res.XmlResourceParser;

public class EarthquakeXmltoBO {
	
	public static final String TAG_EARTHQUAKES = "earthquakes"; 
	public static final String TAG_MAPCENTER = "mapcenter";
	public static final String TAG_EARTHQUAKE = "earthquake";
	public static final String TAG_LATITUDE = "lat";
	public static final String TAG_LONGITIDE = "lon";
	public static final String TAG_DEPTH = "depth";
	public static final String TAG_MAGNITUDE = "mag";
	public static final String TAG_NAME = "name";
	public static final String TAG_STATIONS = "stations";
	public static final String TAG_STATION = "station";
	public static final String TAG_DISTANCE = "dist";
	
	public static List getEarthquakeData ( XmlResourceParser xmlparser){
		List earthquakesList = new ArrayList<EarthQuakes>();
		StringBuffer stringBuffer = new StringBuffer();
		
		
		try {
			xmlparser.next();
			int eventType = xmlparser.getEventType();
			String parentObject = null;
			Stack<String> currentObject = new Stack<String>();
			String currentTagName = null;
			EarthQuakes earthquakes = null;
			MapCenter mapCenter = null;
			Earthquake earthQuake =null;
			List<Station> stations = null;
			Station station = null;
			
			while (eventType != XmlPullParser.END_DOCUMENT){
				if(eventType == XmlPullParser.START_DOCUMENT){
			    	stringBuffer.append("--- Start XML ---");
			    }
			    else if(eventType == XmlPullParser.START_TAG){
			    	stringBuffer.append("\nSTART_TAG: "+xmlparser.getName());
			    	String tagName = xmlparser.getName();
			    	if(tagName!=null && !tagName.equals("")){
						if(TAG_EARTHQUAKES.equals(tagName)){
							earthquakes = new EarthQuakes();
							earthquakesList.add(earthquakes);
							currentObject.push(TAG_EARTHQUAKES);
						}else if(TAG_MAPCENTER.equals(tagName)){
							mapCenter = new MapCenter();
							earthquakes.setMapCenter(mapCenter);
							currentObject.push(TAG_MAPCENTER);
						}
						else if (TAG_EARTHQUAKE.equals(tagName)){
							earthQuake = new Earthquake();
							earthquakes.getEarthQuakes().add(earthQuake);
							currentObject.push(TAG_EARTHQUAKE);
						}
						else if(TAG_STATIONS.equals(tagName)){
							stations = new ArrayList<Station>();
							earthQuake.setStations(stations);
							//currentObject.push(TAG_STATIONS);
						}else if (TAG_STATION.equals(tagName)){
							station = new Station();
							stations.add(station);
							currentObject.push(TAG_STATION);
						}else if (TAG_LATITUDE.equals(tagName)){
							currentTagName = TAG_LATITUDE;
						}else if (TAG_LONGITIDE.equals(tagName)){
							currentTagName = TAG_LONGITIDE;
						}else if (TAG_DEPTH.equals(tagName)){
							currentTagName = TAG_DEPTH;
						}else if (TAG_NAME.equals(tagName)){
							currentTagName = TAG_NAME;
						}
						else if (TAG_MAGNITUDE.equals(tagName)){
							currentTagName = TAG_MAGNITUDE;
						}else if (TAG_DISTANCE.equals(tagName)){
							currentTagName = TAG_DISTANCE;
						}
						
			    	}
			    }
			    else if(eventType == XmlPullParser.TEXT){
			    	stringBuffer.append("\nTEXT: "+xmlparser.getText());
			    	String stringvalue = xmlparser.getText();
			    	 if (TAG_LATITUDE.equals(currentTagName)){
			    		String currentObjectName = currentObject.peek();
			    		Double value = Double.parseDouble(stringvalue);
			    		if(currentObjectName!=null && !currentObjectName.equals("")){
			    			if(TAG_MAPCENTER.equals(currentObjectName)){
			    				mapCenter.setLatitude(value);
			    			}else if (TAG_EARTHQUAKE.equals(currentObjectName)){
			    				earthQuake.setLatitude(value);
			    			}else if (TAG_STATION.equals(currentObjectName)){
			    				station.setLatitude(value);
			    			}
			    		}
			    		currentTagName = null;
					}else if (TAG_LONGITIDE.equals(currentTagName)){
			    		Double value = Double.parseDouble(stringvalue);
			    		String currentObjectName = currentObject.peek();
			    		if(currentObjectName!=null && !currentObjectName.equals("")){
			    			if(TAG_MAPCENTER.equals(currentObjectName)){
			    				mapCenter.setLongitude(value);
			    			}else if (TAG_EARTHQUAKE.equals(currentObjectName)){
			    				earthQuake.setLongitude(value);
			    			}else if (TAG_STATION.equals(currentObjectName)){
			    				station.setLongitude(value);
			    			}
			    		}
			    		currentTagName = null;
					}else if (TAG_DEPTH.equals(currentTagName)){
						Double value = Double.parseDouble(stringvalue);
						String currentObjectName = currentObject.peek();
			    		if(currentObjectName!=null && !currentObjectName.equals("")){
			    			if (TAG_EARTHQUAKE.equals(currentObjectName)){
			    				earthQuake.setDepth(value);
			    			}
			    		}
			    		currentTagName = null;
					}else if (TAG_DISTANCE.equals(currentTagName)){
						Double value = Double.parseDouble(stringvalue);
	    				station.setDistance(value);
			    		
			    		currentTagName = null;
					}
					else if (TAG_NAME.equals(currentTagName)){
						String currentObjectName = currentObject.peek();
			    		if(currentObjectName!=null && !currentObjectName.equals("")){
			    			if (TAG_EARTHQUAKE.equals(currentObjectName)){
			    				earthQuake.setName(stringvalue);
			    			}else if (TAG_STATION.equals(currentObjectName)){
			    				station.setName(stringvalue);
			    			}
			    		}
			    		currentTagName = null;
					}
					else if (TAG_MAGNITUDE.equals(currentTagName)){
						Double value = Double.parseDouble(stringvalue);
						String currentObjectName = currentObject.peek();
			    		if(currentObjectName!=null && !currentObjectName.equals("")){
			    			if (TAG_EARTHQUAKE.equals(currentObjectName)){
			    				earthQuake.setMag(value);
			    			}
			    		}
			    		currentTagName = null;
					}
					
			    	 
			    }
			    else if(eventType == XmlPullParser.END_TAG){
			    	stringBuffer.append("\nEND_TAG: "+xmlparser.getName());		
			    	String tagName = xmlparser.getName();
			    	String top = currentObject.peek();
			    	if(tagName.equals(TAG_EARTHQUAKES)){
			    		earthquakes = null;
			    		if(top!=null && top.equals(TAG_EARTHQUAKES))
			    			currentObject.pop();
					}	
			    	else if(TAG_EARTHQUAKE.equals(tagName)){
			    		earthQuake = null;
			    		if(top!=null && top.equals(TAG_EARTHQUAKE))
			    			currentObject.pop();
					}else if(TAG_MAPCENTER.equals(tagName)){
			    		mapCenter = null;
			    		if(top!=null && top.equals(TAG_MAPCENTER))
			    			currentObject.pop();
					}else if(TAG_STATIONS.equals(tagName)){
						stations = null;
						if(top!=null && top.equals(TAG_STATIONS))
			    			currentObject.pop();
					}else if (TAG_STATION.equals(tagName)){
						station = null;
						if(top!=null && top.equals(TAG_STATION))
			    			currentObject.pop();
					}
			    }
			    eventType = xmlparser.next();
			}
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return earthquakesList;
	}
}
